package com.mall.shopping.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.shopping.constant.ProductConstant;
import com.mall.shopping.constant.ShoppingServiceEnum;
import com.mall.shopping.dto.bo.resp.HomePageResp;
import com.mall.shopping.dto.bo.resp.NavigationResp;
import com.mall.shopping.dto.entity.TbPanel;
import com.mall.shopping.dto.entity.TbPanelContent;
import com.mall.shopping.dto.model.PanelModel;
import com.mall.shopping.dto.model.ProductModel;
import com.mall.shopping.mapper.TbPanelMapper;
import com.mall.shopping.service.TbPanelContentService;
import com.mall.shopping.service.TbPanelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 内容分类 服务实现类
 * </p>
 *
 * @author xrj
 * @since 2022-05-01
 */
@Service
@Component
@Slf4j
public class TbPanelServiceImpl extends ServiceImpl<TbPanelMapper, TbPanel> implements TbPanelService {

    @Autowired
    private TbPanelContentService panelContentService;

    @Autowired
    private TbPanelMapper panelMapper;

    @Override
    public HomePageResp queryHomepageInfo() {
        HomePageResp resp = new HomePageResp();
        List<PanelModel> panelModels = new ArrayList<>();
        try {
            List<TbPanel> panels = list();
            for (TbPanel panel : panels) {
                PanelModel panelModel = new PanelModel();
                BeanUtils.copyProperties(panel, panelModel);
                List<ProductModel> productModels = panelMapper.queryProductById(panel.getId());
                panelModel.setPanelContentItems(productModels);
                panelModels.add(panelModel);
            }
            resp.setPanelModels(panelModels);
            resp.setCode(ShoppingServiceEnum.SUCCESS.getCode());
            resp.setMsg(ShoppingServiceEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            log.error("查询轮播图数据异常", e);
            resp.setCode(ShoppingServiceEnum.DATA_QUERY_ERROR.getCode());
            resp.setMsg(ShoppingServiceEnum.DATA_QUERY_ERROR.getMsg());
        }
        return resp;
    }

    @Override
    public NavigationResp queryPanelContentWithRoot(Integer panelId) {
        NavigationResp resp = new NavigationResp();
        QueryWrapper<TbPanelContent> qw = new QueryWrapper<>();
        qw.eq("panel_id", panelId);
        try {
            List<TbPanelContent> panelContents = panelContentService.list(qw);
            resp.setPanelContents(panelContents);
            resp.setCode(ShoppingServiceEnum.SUCCESS.getCode());
            resp.setMsg(ShoppingServiceEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            log.error("查询导航栏数据异常", e);
            resp.setCode(ShoppingServiceEnum.DATA_QUERY_ERROR.getCode());
            resp.setMsg(ShoppingServiceEnum.DATA_QUERY_ERROR.getMsg());
        }
        return resp;
    }

    @Override
    public HomePageResp queryRecommend() {
        HomePageResp resp = new HomePageResp();
        List<PanelModel> panelModels = new ArrayList<>();
        try {
            TbPanel recommend = getById(ProductConstant.RECOMMEND_CATEGORY);
            PanelModel model = new PanelModel();
            List<ProductModel> productModels = panelMapper.queryProductById(recommend.getId());
            BeanUtils.copyProperties(recommend, model);
            model.setPanelContentItems(productModels);
            panelModels.add(model);
            resp.setPanelModels(panelModels);
            resp.setCode(ShoppingServiceEnum.SUCCESS.getCode());
            resp.setMsg(ShoppingServiceEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            log.error("查询推荐商品数据失败!");
            resp.setCode(ShoppingServiceEnum.DATA_QUERY_ERROR.getCode());
            resp.setMsg(ShoppingServiceEnum.DATA_QUERY_ERROR.getMsg());
        }
        return resp;
    }
}
